package org.jeecg.modules.scm.controller;

import cn.hutool.core.util.URLUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.modules.scm.entity.ScmCusVisit;
import org.jeecg.modules.scm.service.IScmCusVisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: scm_cus_visit
 * @Author: jeecg-boot
 * @Date: 2021-11-29
 * @Version: V1.0
 */
@Api(tags = "客户走访")
@RestController
@RequestMapping("/scmCusVisit/scmCusVisit")
@Slf4j
public class ScmCusVisitController extends JeecgController<ScmCusVisit, IScmCusVisitService> {
    @Autowired
    private IScmCusVisitService scmCusVisitService;


    @AutoLog(value = "完成走访")
    @ApiOperation(value = "完成走访")
    @PostMapping(value = "/finishVisit")
    public Result<?> finishVisit(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.getString("id");
        String userLocation = jsonObject.getString("userLocation");
        ScmCusVisit cusVisit = scmCusVisitService.getById(id);
        if(cusVisit.getStatus().equals(2)){
            return Result.error("该走访已完成，请勿重复提交！");
        }
        String location = cusVisit.getLocation();
        if (location.equals(userLocation)) {
            cusVisit.setStatus(2);
            cusVisit.setEndTime(new Date());
            boolean b = scmCusVisitService.updateById(cusVisit);
            if (b) {
                return Result.OK("完成走访成功！");
            } else {
                return Result.error("完成走访失败，请重试！");
            }
        } else {
            return Result.error("距离客户所在地过远！");
        }
    }

    @AutoLog(value = "附件上传")
    @ApiOperation(value = "附件上传")
    @PostMapping(value = "/uploadAccessory")
    public Result<?> uploadContract(@RequestParam(name = "file") MultipartFile file) {
        String url = CommonUtils.upload(file, "/afterLoan/accessory", CommonConstant.UPLOAD_TYPE_MINIO);
        String path = URLUtil.getPath(url);
        return Result.OK(path);
    }

    /**
     * 分页列表查询
     *
     * @param scmCusVisit
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "客户走访-分页列表查询")
    @ApiOperation(value = "客户走访-分页列表查询", notes = "客户走访-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(ScmCusVisit scmCusVisit,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<ScmCusVisit> queryWrapper = QueryGenerator.initQueryWrapper(scmCusVisit, req.getParameterMap());
        Page<ScmCusVisit> page = new Page<ScmCusVisit>(pageNo, pageSize);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (!user.getOrganization().getStatus().equals(1)) {
            queryWrapper.eq("cus_no", user.getCusNo());
        }
        IPage<ScmCusVisit> pageList = scmCusVisitService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param scmCusVisit
     * @return
     */
    @AutoLog(value = "客户走访-添加")
    @ApiOperation(value = "客户走访-添加", notes = "客户走访-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody ScmCusVisit scmCusVisit) {
        scmCusVisitService.save(scmCusVisit);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param scmCusVisit
     * @return
     */
    @AutoLog(value = "客户走访-编辑")
    @ApiOperation(value = "客户走访-编辑", notes = "客户走访-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody ScmCusVisit scmCusVisit) {
        scmCusVisitService.updateById(scmCusVisit);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "客户走访-通过id删除")
    @ApiOperation(value = "客户走访-通过id删除", notes = "客户走访-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        scmCusVisitService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "客户走访-批量删除")
    @ApiOperation(value = "客户走访-批量删除", notes = "客户走访-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.scmCusVisitService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "客户走访-通过id查询")
    @ApiOperation(value = "客户走访-通过id查询", notes = "客户走访-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        ScmCusVisit scmCusVisit = scmCusVisitService.getById(id);
        if (scmCusVisit == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(scmCusVisit);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param scmCusVisit
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ScmCusVisit scmCusVisit) {
        return super.exportXls(request, scmCusVisit, ScmCusVisit.class, "scm_cus_visit");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ScmCusVisit.class);
    }

}
